package com.woniu.controller;

import com.woniu.pojo.*;
import com.woniu.service.ClazzService;
import com.woniu.service.LessonService;
import com.woniu.service.SchoolService;
import com.woniu.service.UserService;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.context.request.WebRequest;

import javax.annotation.Resource;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("EduManage")//教务管理
public class LessonController {
    @Resource
    private LessonService lessonService;
    @Resource
    private SchoolService schoolService;
    @Resource
    private ClazzService clazzService;
    @Resource
    private UserService userService;

    /**
     *  跳转页面
     *  给前端返回需要的数据
     *
     * @param model
     * @return
     */
    @RequestMapping("lessonList")
    public String lessonList(Model model){
        //school列表
        List<School> schoolList=new ArrayList<>();
        schoolList = schoolService.findAllSch();
        model.addAttribute("schoolList",schoolList);
        //class列表
        List<Clazz> clazzList=new ArrayList<>();
        clazzList =clazzService.findAllClazz();
        model.addAttribute("clazzList",clazzList);
        //teacher列表
        List<User> userList=new ArrayList<>();
        userList=userService.findTeacher();
        model.addAttribute("userList",userList);
        return "EduManage/lessonList";

    }

    /**
     *  课程表的展示
     * @param pageBean
     * @param lesson
     * @return
     */
    @RequestMapping("lessonListDo")
    @ResponseBody
    public Map<String,Object> lessonListDo(PageBean pageBean, Lesson lesson){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            List<Lesson> lessonList=lessonService.getLessonList(pageBean, lesson);
            resultMap.put("lessonList", lessonList);
            resultMap.put("pageBean", pageBean);
            resultMap.put("result", true);
        } catch (Exception e) {
            resultMap.put("result", false);
        }
        return resultMap;
    }
    /**
     * 删除学生
     * @param lno
     * @return
     */
    @RequestMapping("delDo")
    @ResponseBody
    public Map<String,Object> delDo(Integer lno){
        Map<String,Object> resultMap=new HashMap<>();
        try {
            lessonService.delByLesson(lno);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    /**
     * 课程新增
     * @param
     * @return
     */
    @RequestMapping("lessonSaveDo")
    @ResponseBody
    public Map<String,Object> lessonSaveDo(Lesson lesson){
        Map<String,Object> resultMap = new HashMap<>();
        try{
            lessonService.saveLesson(lesson);
            resultMap.put("result",true);
        }catch (Exception e){
            resultMap.put("result",false);
        }
        return resultMap;
    }
    @InitBinder
    public void initBinder(WebDataBinder binder, WebRequest request) {
        //转换日期
        DateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd");
        binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));// CustomDateEditor为自定义日期编辑器
    }
}
